#include "latchandnixie.h"

static unsigned char nixie_i[9] = {0,20,20,20,20,20,20,20,20};
unsigned char code nixie_number[22] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xff,0xbf};

void Latch(unsigned char p2,unsigned char p0)
{
	P2 = (P2 & 0x1f | p2);
	P0 = p0;
	P2 &= 0x1f;
}

void nixie_show(unsigned char wei,unsigned char number)
{
	nixie_i[wei] = number;
}

void nixieshow(unsigned char wei,unsigned char number)
{
	Latch(NIXIE_D,0xff);
	//Latch(NIXIE_W,0x01 << (wei - 1));
	switch(wei)
{
	case 1:Latch(NIXIE_W,0x01);break;
	case 2:Latch(NIXIE_W,0x02);break;
	case 3:Latch(NIXIE_W,0x04);break;
	case 4:Latch(NIXIE_W,0x08);break;
	case 5:Latch(NIXIE_W,0x10);break;
	case 6:Latch(NIXIE_W,0x20);break;
	case 7:Latch(NIXIE_W,0x40);break;
	case 8:Latch(NIXIE_W,0x80);break;
}
	Latch(NIXIE_D,nixie_number[number]);
}

void nixie_loop(void)
{
	static unsigned char n = 1;
	nixieshow(n,nixie_i[n]);
	if(++n == 10)
		n = 1;
}